use strict;
use Stdlog;
use File::Spec;
use Test::More tests => 6;

our $stderr;

# Test 1 -- use Stdlog::Writer::STDERR
BEGIN { use_ok('Stdlog::Writer::STDERR');}

# Test 2 -- create STDERR writer
my $writer = Stdlog::Writer->new({writer => 'STDERR'});
ok($writer && ref($writer) eq 'Stdlog::Writer::STDERR',
    "Created Stdlog::Writer::STDERR");

# Test 3 -- output a simple scalar without \n
reset_stderr();
$writer->logmsg({}, "foo bar");
ok($stderr eq "foo bar\n",
    "Simple scalar (no newline) output" );

# Test 4 -- output a simple scalar with \n
reset_stderr();
$writer->logmsg({}, "foo bar\n");
ok($stderr eq "foo bar\n",
    "Simple scalar (with newline) output" );

# Test 5 -- output multiple scalars without \n
reset_stderr();
$writer->logmsg({}, "foo", "bar", "baz");
ok($stderr eq "foo bar baz\n",
    "Multiple scalars (without newline) ooutput" );


# Test 6 -- output multiple scalars with \n
reset_stderr();
$writer->logmsg({}, "foo", "bar", "baz\n");
ok($stderr eq "foo bar baz\n",
    "Multiple scalars (with newline) ooutput" );






# close STDERR and redirect it to the $stderr string buffer
sub reset_stderr {
    $stderr = "";
    close STDERR;
    open STDERR, '>', \$stderr;
}